package subscriberPanels;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;

import clientGUI.ConnectionGUI;
import clientGUI.MainGUI;
import clientGUI.PanelDesign;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ExtendBorrowPanel extends PanelDesign {  
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static JTable table;
	private String updateReturnDate ; 
	public JButton btnExtend;
	/**
	 * Create the panel.
	 */
	public ExtendBorrowPanel() {
		setLayout(null);
		setPreferredSize(new Dimension(644, 462));
		
		JLabel lbltitle = new JLabel("Choose which books you want to extend from your list:");
		lbltitle.setFont(new Font("Tahoma", Font.BOLD, 13));
		lbltitle.setBounds(150, 22, 403, 28);
		add(lbltitle);
		
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);  
		table.setModel(new DefaultTableModel(
				new Object[][] {
					{null, null, null}, {null, null, null},
					{null, null, null}, {null, null, null},
					{null, null, null}, {null, null, null},
					{null, null, null}, {null, null, null},
					{null, null, null}, {null, null, null},
					{null, null, null}, {null, null, null},
			
				},
				new String[] {
					"Book Name", "Borrow Date" ,"Expected Return Date" 
				}));

		table.setBounds(52, 50, 326, 170);	

		
		JScrollPane test = new JScrollPane(table);
		test.setBounds(120, 61, 421, 199);
		add(test);
	
		
		String[] s=new String[3];
		s[0]="ExtendBorrow";
		s[1]="WantToExtend";
		s[2]= ConnectionGUI.user.id;
		MainGUI.OBLClient.handleMessageFromClient(s);
		
		
		btnExtend = new JButton("Extend");
		btnExtend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(table.getSelectedRow()==(-1))
					{
						if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(new JFrame(), "Choose a book for extension");
						return ; 
					}
				String[] s=new String[6];
				String bookName=(String) table.getValueAt(table.getSelectedRow(), 0);
				s[0]="ExtendBorrow";
				s[1]=bookName;   // book name
				s[2]= ConnectionGUI.user.id;
				String borrowDate=(String) table.getValueAt(table.getSelectedRow(), 1);
				String returnDate=(String) table.getValueAt(table.getSelectedRow(), 2);
				s[3]=borrowDate;
				
				if(isWeekBefor(returnDate))
				{
					s[4]=updateReturnDate;
					s[5]=ConnectionGUI.user.firstName+" "+ConnectionGUI.user.lastName; 
					MainGUI.OBLClient.handleMessageFromClient(s);
				}
				else 
					{
						if(!MainGUI.testmode)
						JOptionPane.showMessageDialog(new JFrame(), "You can extend only week or less before return date");								
					}
			}
		});
		
		
		
		btnExtend.setBounds(270, 271, 89, 23);
		add(btnExtend);
		remove(background);
		add(background);

	}
	
	
	
	/**
	 * This function calculate the difference between two days.
	 * @param returnDate - string with the date that the subscriber need to return the book 
	 * @return true-if he should ask for extension false if it doesn't a week before the expected return date-can't ask for extension 
	 */
	private Boolean isWeekBefor(String returnDate)
	{
		Calendar isWeekBefore=Calendar.getInstance();   // today
		isWeekBefore.add(Calendar.DATE, 7);
		DateFormat formatter=new SimpleDateFormat("yyyy/MM/dd");
		try 
		{
			Date date = (Date)formatter.parse(returnDate);
			Calendar cal=Calendar.getInstance();
			cal.setTime(date);
			if(isWeekBefore.compareTo(cal)>=0)  // check that the extension is week or less before return day	
			{
				cal.add(Calendar.DATE, 14);   // update return date to date+14
				updateReturnDate = "'"+ formatter.format(cal.getTime()) + "'";
				return true ; 
				
			}
			else return false ; 
		}
		catch (ParseException e1) { e1.printStackTrace(); return false;  }
	}
	
	
	
	/**
	 * Initialize the data of the table in this panel with the given Matrix.
	 * @param x
	 */
	public static void changeTableContent(String[][] x)
	{
		table.setModel(new DefaultTableModel(x,	new String[] {
				"Book Name", "Borrow Date" ,"Expected Return Date" 
			}){
		private static final long serialVersionUID = 1L;
		public boolean isCellEditable(int row, int column) {
			return false;
		}
		});
		table.getTableHeader().setReorderingAllowed(false); 
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
	}
	

	
	
}
